package com.ucode.tool.base;

import java.io.Serializable;

/**
 * 统一响应返回结果类
 * 状态码各个模块服务约定:
 * 1、所有服务的公共状态码枚举在ResultCodeEnum枚举
 * 2、其他服务自定义的状态码，由服务代码(4位)+状态码(4位)组成，各个服务代码如下： 
 * 会员中心:1000 例如：10000001
 * 支付服务:2000 例如：20000001
 * @author liliang
 * @date: 2019年12月6日 上午11:05:25
 */
public class ResponseResult<T> implements Serializable{

    private static final long serialVersionUID = 6053598451919328042L;

    /**
     * 状态码：1成功，其他为失败
     */
    public int code;

    /**
     * 成功为success，其他为失败原因
     */
    public String message;

    /**
     * 数据结果集
     */
    public T data;
    
    public ResponseResult(){}

    public ResponseResult(int code, String message, T data) {
        super();
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static <T> ResponseResult<T> success() {
        return result(ResultCodeEnum.SUCCESS.code, ResultCodeEnum.SUCCESS.message, null);
    }
    
    public static <T> ResponseResult<T> success(String message,T data) {
        return result(ResultCodeEnum.SUCCESS.code, message, data);
    }
    
    public static <T> ResponseResult<T> success(T data) {
        return result(ResultCodeEnum.SUCCESS, data);
    }
    
    public static <T> ResponseResult<T> failed() {
        return result(ResultCodeEnum.FAILED.code, ResultCodeEnum.FAILED.message, null);
    }
    
    public static <T> ResponseResult<T> failed(String message,T data) {
        return result(ResultCodeEnum.FAILED.code, message, data);
    }
    
    public static <T> ResponseResult<T> failed(T data) {
        return result(ResultCodeEnum.FAILED, data);
    }
    
    public static <T> ResponseResult<T> result(ResultCodeEnum resultCodeEnum) {
        return result(resultCodeEnum, null);
    }
    
    public static <T> ResponseResult<T> result(ResultCodeEnum resultCodeEnum,T data) {
        return result(resultCodeEnum.code, resultCodeEnum.message, data);
    }
    
    public static <T> ResponseResult<T> result(Integer code, String message,T data) {
        return new ResponseResult<>(code, message, data);
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}